﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.LuFang.API.Application.Command.RoleCommand;
using RBAC.LuFang.API.Dtos;
using RBAC.LuFang.Domain;
using RBAC.LuFang.Domain.DTO;
using RBAC.LuFang.Domin.Services;
using System.Collections.Generic;
using System.Security.Claims;

namespace RBAC.LuFang.API.Controllers
{
    /// <summary>
    /// 角色控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _roleService;
        private readonly IMediator _mediatR;
        //构造函数
        public RoleController(IRoleService roleService, IMediator mediatR)
        {
            _roleService = roleService;
            _mediatR = mediatR;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddRole(RoleCreateCommand command)
        {
            ApiResult<int> result=new ApiResult<int>();
            try
            {
                var data = HttpContext.User;//获取当前登录用户的所有ToKen信息
                List<int> ls = new List<int>();
                var a = ls.Count;
                var id = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取sub的值
                var role = HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;//获取角色信息的值
                var userdata = HttpContext.User.FindFirst(ClaimTypes.UserData)?.Value;//用户自定义的数据
                result.Data=await _mediatR.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Page>> GetAllRoles([FromQuery]RoleSelectCommand role)
        {
            ApiResult<Page> result=new ApiResult<Page>();
            try
            {
                result.Data = await _mediatR.Send(role);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelRole(RoleDeleteCommand id)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediatR.Send(id);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 角色反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<Role>> GetByid(RoleFanCommand id)
        {
            ApiResult<Role> result = new ApiResult<Role>();
            try
            {
                result.Data = await _mediatR.Send(id);
            }
            catch (Exception ee)
            {
                result.Code= -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdRole(RoleUpdateCommand role)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediatR.Send(role);
            }
            catch (Exception ee)
            {
                result.Code=-1;
                result.Message=ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 根据部门ID查询员工名称
        /// </summary>
        /// <param name="sectionId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<GetemployeeDTO>>> Getemployee([FromQuery]GettemployeeCommand command)
        {
            ApiResult<List<GetemployeeDTO>> result= new ApiResult<List<GetemployeeDTO>>();
            try
            {
                result.Data = await _mediatR.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message=ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 添加用户角色
        /// </summary>
        /// <param name="userRole"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddUserRole(UserRoleCreateCommand userroles)
        {
            ApiResult<int> result= new ApiResult<int>();
            try
            {
                result.Data = await _mediatR.Send(userroles);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message=ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填部门树形
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<GetRoleDTO>>> GetRole([FromQuery]DepartmentFanCommand command)
        {
            ApiResult<List<GetRoleDTO>> result = new ApiResult<List<GetRoleDTO>>();
            try
            {
                result.Data = await _mediatR.Send(command);
            }
            catch (Exception ee)
            {
                result.Code= -1;
                result.Message=ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示某角色的员工
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<GetRoleNameDTO>>> GetRoleName([FromQuery]RoleEmployeeCommand role)
        {
            ApiResult<List<GetRoleNameDTO>> result = new ApiResult<List<GetRoleNameDTO>>();
            try
            {
                result.Data = await _mediatR.Send(role);

            }
            catch (Exception ee)
            {
                result.Code=-1;
                result.Message=ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除角色下的员工
        /// </summary>
        /// <param name="urId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Del(RoleEmployeeDeleteCommand role)
        {
            ApiResult<int>  result = new ApiResult<int>();
            try
            {
                result.Data = await _mediatR.Send(role);

            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message=ee.Message;
                throw;
            }
            return result;
        }















    }
}
